package com.dubbo;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.rpc.service.GenericService;

import java.util.HashMap;
import java.util.Map;

/**
 * @author afei
 * @version 1.0.0
 * @since 2017年11月22日
 */
public class Main {

    public static void main(String[] args) {
        // 普通编码配置方式
        ApplicationConfig application = new ApplicationConfig();
        application.setName("kwg-dsp-asset");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2189");

        // 引⽤远程服务, 该实例⾥⾯封装了所有与注册中⼼及服务提供⽅连接，请缓存
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        // 弱类型接⼝名
        reference.setInterface("com.kwggroupholdings.dsp.asset.api.PointQueryService");
        reference.setVersion("1.0.1");
        // 声明为泛化接⼝
        reference.setGeneric(true);
        // ⽤com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用⽤
        GenericService genericService = reference.get();
        // 基本类型以及Date,List,Map等不需要转换，直接调⽤
//        Object result = genericService.$invoke("id", new String[] {"java.lang.String"}, new Object[] {"afei"});
//        System.out.println("result --> "+result);

        // ⽤Map表示POJO参数，如果返回值为POJO也将自动转成Map
        Map<String, Object> teacher = new HashMap<String, Object>();
        teacher.put("id", "0000642b9795493fb676f496d16ca954");
        // 如果返回POJO将自动转成Map
        Object result = genericService.$invoke("findById", new String[]
                {"com.kwggroupholdings.dsp.asset.api.PointQueryService"}, new Object[]{teacher});
        System.out.println("result --> "+result);
    }
}